﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.IO;

namespace IHISCETeim.Web.MachineSys.JSDWF
{
    public partial class JSUserDWFView : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "JSDWFList"; }
        }

        protected int showtype
        {
            get
            {
                return GetRequestValue<int>("showtype", 0);
            }
        }

        protected string DWFID
        {
            get
            {
                return GetRequestValue<string>("DWFID", "");
            }
        }

        private JSDWFInfo _DWFobj = null;
        protected JSDWFInfo DWFobj
        {
            get
            {
                if (_DWFobj == null)
                {
                    _DWFobj = JSDWFController.GetByDWFID(new Guid(DWFID));
                    _DWFobj = _DWFobj == null ? new JSDWFInfo() : _DWFobj;
                }

                return _DWFobj;
            }
        }

        private JSDWFFolderInfo _folderObj = null;
        protected JSDWFFolderInfo DWFFolder
        {
            get
            {
                //   JSDWFFolderInfo _folderObj = JSDWFFolderController.GetByFolderID(FolderID);
                if (_folderObj == null)
                {
                    if (DWFobj.FolderID == 0)
                        _folderObj = new JSDWFFolderInfo() { FolderName = "图纸根目录", FolderPath = JSDWFFolderController.DWFUploadFolderPath };
                    else
                        _folderObj = JSDWFFolderController.GetByFolderID(DWFobj.FolderID);
                }

                return _folderObj;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<int>("showdwf", 0) == 1)
            {
                getshowdwf();
            }
        }

        private void getshowdwf()
        {
            HttpContext context = HttpContext.Current;
            string filepath = GetDwfPath();    //文件路径          
            int alllen = filepath.Length;
            int fileanemstate = filepath.LastIndexOfAny(new char[] { '/' });
            string filename = filepath.Substring(fileanemstate + 1, alllen - fileanemstate - 1);
            //设置输出文件类型为DWF格式
            context.Response.ContentType = "drawing/x-dwf";
            //添加下载头信息，并设置下载文件名称
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            //下载文件
            context.Response.TransmitFile(filepath);

        }


        protected string GetDwfPath()
        {
            return DWFFolder.FolderPath + "/" + DWFobj.FileName;
        } 
    }
}